package com.tonicgb.shared.core.crud.mybatis.mapper.provider;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.tonicgb.shared.core.crud.entity.BaseEntity;
import org.apache.ibatis.jdbc.SQL;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;


/**
 * @author zhaiyujin
 * @date 2021-09-01 21:52:53
 * @说明 curd Sql语句构建
 */
public class SelectSqlProvider {

    private String SqlProvider(BaseEntity<?> entity) {
        Class<?>c=entity.getClass();
        Field[] fields=c.getDeclaredFields();
        String table;

        Annotation annotation=c.getAnnotation(TableId.class);
        TableName tableName=(TableName)c.getAnnotation(TableName.class);
        if(tableName==null){
            table= (entity.getClass().getSimpleName()).toLowerCase();
        }else{
            table=tableName.value();
        }
        String finalTable = table;
        return new SQL(){{
            boolean hasPrimaryKey = false;
            SELECT("*");
            FROM(finalTable);
            for (Field field : fields) {
                String tableFields;
                field.setAccessible(true);

                Object o= null;
                try {
                    o = field.get(entity);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                if(o==null||StringUtils.isBlank(o.toString())) continue;
                if(!hasPrimaryKey){
                    TableId tableId=(TableId)field.getAnnotation(TableId.class);
                    if(tableId!=null){
                        tableFields=tableId.value();
                        hasPrimaryKey=true;
                        WHERE(tableId.value()+" = #{"+tableId.value()+"}");
                        continue;
                    }
                }
                TableField tableField=(TableField)field.getAnnotation(TableField.class);

                if(tableField==null){
                    tableFields=field.getName();
                }else {
                    tableFields= tableField.value();
                }

                WHERE(StringUtils.camelToUnderline(tableFields)+" = #{"+tableFields+"}");

            }
        }}.toString();



    }
    public String get(BaseEntity<?> entity){
        return this.SqlProvider(entity) + " limit 1 ";
    }
    public String getByEntity(BaseEntity<?> entity){
        return this.SqlProvider(entity);
    }
    public String findCount(BaseEntity<?> entity){
     //   return (new StringBuilder()).insert(0, "/*return count*/").append(this.findList(entity)).toString();
        return (new StringBuilder()).append(this.findList(entity)).replace(7, 8,"count(*)").toString();
    }
    public String findList(BaseEntity<?> entity){
        return this.SqlProvider(entity);
    }

    public String findPage(BaseEntity<?> entity){

        Class<?> c=entity.getClass().getSuperclass();
        int pageNo=1;
        int pageSize=10;
        try {
            Field pageNoField=c.getDeclaredField("pageNo");
            Field pageSizeField=c.getDeclaredField("pageSize");
            Object o1=pageNoField.get(entity);
            Object o2 =pageSizeField.get(entity);
            pageNo= o1==null? pageNo: Integer.parseInt(String.valueOf(o1));
            pageSize= o2==null? pageNo: Integer.parseInt(String.valueOf(o2));

        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }

       return (new StringBuilder()).append(this.findList(entity)).append(" limit "+pageNo+","+pageSize).toString();


    }

}
